#include "aligned_memory.h"
#include "platform_abstraction.h"
#include <cstring>

namespace Base {
namespace Utils {

AlignedMemory::AlignedMemory(size_t size, size_t alignment)
    : size_(size), alignment_(alignment) {
    ptr_ = Platform::MemoryAllocateAligned(size, alignment);
}

AlignedMemory::~AlignedMemory() {
    if (ptr_) {
        Platform::MemoryFree(ptr_);
    }
}

AlignedMemory::AlignedMemory(AlignedMemory&& other) noexcept
    : ptr_(other.ptr_), size_(other.size_), alignment_(other.alignment_) {
    other.ptr_ = nullptr;
    other.size_ = 0;
    other.alignment_ = 0;
}

AlignedMemory& AlignedMemory::operator=(AlignedMemory&& other) noexcept {
    if (this != &other) {
        if (ptr_) {
#ifdef _WIN32
            _aligned_free(ptr_);
#else
            free(ptr_);
#endif
        }
        
        ptr_ = other.ptr_;
        size_ = other.size_;
        alignment_ = other.alignment_;
        
        other.ptr_ = nullptr;
        other.size_ = 0;
        other.alignment_ = 0;
    }
    return *this;
}

} // namespace Utils
} // namespace Base
